package com.log.blog_backend.controller;

import com.log.blog_backend.Dao.ArticleDetail;
import com.log.blog_backend.common.Result;
import com.log.blog_backend.entity.Article;
import com.log.blog_backend.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/article")
@CrossOrigin
@Slf4j
public class ArticleController {
    @Autowired
    private  ArticleService articleService;

    @PostMapping("savePage")
    public Result savePage(@RequestBody Article article, HttpServletRequest httpServletRequest){

        return articleService.savePage(article,httpServletRequest);
    }
    @PostMapping("uploadArticle")
    public Result uploadArticle(@RequestBody Article article, HttpServletRequest httpServletRequest){

        return articleService.uploadArticle(article,httpServletRequest);
    }

    @GetMapping("getAllArticles")
    public Result getAllArticles(HttpServletRequest httpServletRequest){
        return articleService.getAllArticles(httpServletRequest);

    }
    @GetMapping("getHotpotArticles")
    public Result getHotpotArticles(){
        return articleService.getHotpotArticles();

    }
    @DeleteMapping("deleteArticleById")
    public Result deleteArticleById(@RequestBody String articleId){
        boolean flag = articleService.removeById(articleId);
        if(flag){
            return Result.ok();
        }else {
            return Result.fail("删除失败",6000);
        }

    }
    @GetMapping("getArticleById/{articleId}")
    public Result getArticleById(@PathVariable("articleId") Long articleId){
        Article article = articleService.getArticle(articleId);
        if(article!=null){
            return Result.ok(article);
        }else {
            return Result.fail("文章不存在",404);
        }

    }
    @GetMapping("getArticleListBySearchContent/{searchContent}")
    public Result getArticleListBySearchContent(@PathVariable("searchContent") String searchContent){
        return articleService.getArticleListBySearchContent(searchContent);
    }
    @GetMapping("countUserReleaseArticle/{userId}")
    public Result countUserReleaseArticle(@PathVariable("userId") Long userId){

        return  articleService.countUserReleaseArticle(userId);

    }
}
